package com.hua.api.core.framework.http.util;

import com.alibaba.fastjson.JSON;

import com.hua.api.core.framework.http.exception.SgOpenException;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.lang.reflect.Field;
import java.util.*;

@SuppressWarnings("all")
public class ConvertUtil {
    public ConvertUtil() {
    }

    public static Map<String, String> convertToMap(Object object) throws Exception {
        if (object == null) {
            return Collections.emptyMap();
        } else {
            Map<String, String> resultMap = new HashMap();
            Field[] fields = object.getClass().getDeclaredFields();
            if (fields != null && fields.length != 0) {
                Field[] var3 = fields;
                int var4 = fields.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    Field field = var3[var5];
                    field.setAccessible(true);
                    Class typeClazz = field.getType();
                    String key = field.getName();
                    String val = null;
                    if (List.class.isAssignableFrom(typeClazz)) {
                        val = JSON.toJSONString(field.get(object));
                    } else {
                        val = String.valueOf(field.get(object));
                    }

                    if (val != null && !"".equals(val) && !"null".equals(val) && !"NULL".equals(val)) {
                        resultMap.put(key, val);
                    }
                }

                return resultMap;
            } else {
                return Collections.emptyMap();
            }
        }
    }



    public static List<BasicNameValuePair> convertToEntity(Map<String, String> applicationParamsMap) throws SgOpenException {
        ArrayList formParam = new ArrayList();

        try {
            if (applicationParamsMap != null) {
                Iterator var2 = applicationParamsMap.entrySet().iterator();

                while(var2.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry)var2.next();
                    BasicNameValuePair nameValuePair = new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue());
                    formParam.add(nameValuePair);
                }
            }

            return formParam;
        } catch (Exception var5) {
            throw new SgOpenException(var5);
        }
    }

    public static List<BasicNameValuePair> convertToEntityOfOAuth(Map<String, String> applicationParamsMap) {
        List<BasicNameValuePair> formParam = new ArrayList();
        if (applicationParamsMap != null) {
            Iterator var2 = applicationParamsMap.entrySet().iterator();

            while(var2.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry)var2.next();
                BasicNameValuePair nameValuePair = new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue());
                formParam.add(nameValuePair);
            }
        }

        return formParam;
    }

    public static List<NameValuePair> convertToEntityBasic(Map<String, String> applicationParamsMap) throws SgOpenException {
        ArrayList formParam = new ArrayList();

        try {
            if (applicationParamsMap != null) {
                Iterator iterator = applicationParamsMap.entrySet().iterator();

                while(iterator.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry)iterator.next();
                    BasicNameValuePair nameValuePair = new BasicNameValuePair((String)entry.getKey(), JSON.toJSONString(entry.getValue()));
                    formParam.add(nameValuePair);
                }
            }

            return formParam;
        } catch (Exception var5) {
            throw new SgOpenException(var5);
        }
    }
}
